package com.zero.zmodel
{
	import flash.utils.Dictionary;
	
	import com.zero.zutils.BaseUtils;
	import com.zero.zutils.ClassUtils;
	import com.zero.zutils.ObjectUtils;

	public class ComponentModel
	{
		public function ComponentModel()
		{
			_componentStyle = new Dictionary();
			_customObjectStyle = new Dictionary();
			_styleSets = new Dictionary();
			_shaderData = new Dictionary();
			_bitmapSets = new Dictionary();
			_loadedComponentSet = new Dictionary();
		}
		
		private var _bitmapSets:Dictionary;
		private var _componentStyle:Dictionary;
		private var _currentComponentSet:XML;
		private var _customObjectStyle:Dictionary;
		private var _loadedComponentSet:Dictionary;
		private var _shaderData:Dictionary;
		private var _styleSets:Dictionary;
		
		public function addBitmapSet(classpath:String,tagData:XML):void
		{
			_bitmapSets[classpath] = tagData;
		}
		
		public function addComponentSet(componentSet:XML):void
		{
			if(_loadedComponentSet[String(componentSet.@name)])return;
			_currentComponentSet = componentSet;
			_loadedComponentSet[String(_currentComponentSet.@name)] = new Dictionary();
			paras();
		}
		/**
		 * 
		 * @param classpath 位图的路径
		 * @return 位图配置 
		 * 
		 */		
		public function getBitmapSet(classpath:String):XML
		{
			return _bitmapSets[classpath];
		}
		/**
		 * 
		 * @param stylename Component配置的样式名称
		 * @return Component配置
		 * 
		 */		
		public function getComonentStyle(stylename:String):XML
		{
			return _componentStyle[stylename];
		}
		
		
		public function getCustomObjectStyle(stylename:String):XML
		{
			return _customObjectStyle[stylename];
		}
		/**
		 * 
		 * @param key 公用的配置的样式名称
		 * @return 公用的配置
		 * 
		 */		
		public function getSet(key:String):*
		{
			return _styleSets[key];
		}
		
		private function paras():void
		{
			parasSets(_currentComponentSet..set);
			parasComponent(_currentComponentSet..component);
			parasCustomObject(_currentComponentSet..custom);
			parasBitmapDataSets(_currentComponentSet..bitmapData);
			parasBitmapSets(_currentComponentSet..bitmap);
		}
		
		private function parasBitmapDataSets(list:XMLList):void
		{
			var len:int = list.length();
			for(var i:int = 0;i<len;i++)
			{
				_bitmapSets[String(list[i].@resourceLink)] = list[i];
				list[i].@stylename = String(list[i].@resourceLink)
				_loadedComponentSet[String(_currentComponentSet.@name)][String(list[i].@resourceLink)] = list[i];
			}
		}
		
		private function parasBitmapSets(list:XMLList):void
		{
			var len:int = list.length();
			for(var i:int = 0;i<len;i++)
			{
				_bitmapSets[String(list[i].@resourceLink)] = list[i];
			}
		}
		
		private function parasComponent(list:XMLList):void
		{
			var len:int = list.length();
			for(var i:int = 0;i < len;i++)
			{
				if(_componentStyle[String(list[i].@stylename)] != null)
				{
					var error:Error = new Error("样式重名。。。请注意检查!!!!!!!!"+String(list[i].@stylename));
					throw error;
				}
				_componentStyle[String(list[i].@stylename)] = list[i];
				_loadedComponentSet[String(_currentComponentSet.@name)][String(list[i].@stylename)] = list[i];
			}
		}
		
		private function parasCustomObject(list:XMLList):void
		{
			for(var i:int = 0;i<list.length();i++)
			{
				if(_customObjectStyle[String(list[i].@stylename)] != null)
				{
					var error:Error = new Error("样式重名。。。请注意检查!!!!!!!!"+String(list[i].@stylename));
					throw error;
				}
				_customObjectStyle[String(list[i].@stylename)] = list[i];
				_loadedComponentSet[String(_currentComponentSet.@name)][String(list[i].@stylename)] = list[i];
			}
		}
		
		private function parasSets(list:XMLList):void
		{
			var len:int = list.length();
			for(var i:int = 0;i<len;i++)
			{
				if(String(list[i].@type) == ClassUtils.COLOR_MATIX_FILTER)
				{
					_styleSets[String(list[i].@stylename)] = ClassUtils.CreatInstance(list[i].@type,[BaseUtils.stringToArray(list[i].@args)]);
				}else
				{
					_styleSets[String(list[i].@stylename)] = ClassUtils.CreatInstance(list[i].@type,BaseUtils.stringToArray(list[i].@args));
				}
				ObjectUtils.copyPorpertiesByXML(_styleSets[String(list[i].@stylename)],list[i]);
				_loadedComponentSet[String(_currentComponentSet.@name)][String(list[i].@stylename)] = list[i];
			}
		}
		
		public function getComponentConfig(stleName:String):Array
		{
			for(var file:String in _loadedComponentSet)
			{
				for(var style:String in _loadedComponentSet[file])
				{
					if(style == stleName)
						return [file,_loadedComponentSet[file][style]];
				}
			}
			return null;
		}
		
		public function deleteComponentConfig(stleName:String):Array
		{
			for(var file:String in _loadedComponentSet)
			{
				for(var style:String in _loadedComponentSet[file])
				{
					if(style == stleName)
					{
						delete _loadedComponentSet[file][style];
						return [file,_loadedComponentSet[file][style]]
					}
				}
			}
			return null;
		}
		
		public function get loadedComponentSet():Dictionary
		{
			return _loadedComponentSet;
		}
	}
}